<template>
  <div class='container'>
    <el-dialog :visible="isAddDirectoryDialog" title="新增目录" width="400px" @close="onClose">
      <el-form ref="ruleForm" :model="form" :rules="rules">
        <el-form-item v-if="!this.$route.query.id" label="所属学科">
          <el-select v-model="form.subjectID" placeholder="请选择">
            <el-option
                v-for="item in subjectsSimple"
                :key="item.value"
                :label="item.label"
                :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="目录名称" prop="directoryName">
          <el-input v-model="form.directoryName" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="onClose()">取 消</el-button>
        <el-button type="primary" @click="DirectoryAdd">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addDirectory } from '../../api/hmmm/directorys'

export default {
  name: 'AddDirectors',
  props: {
    isAddDirectoryDialog: {
      type: Boolean,
      required: true
    },
    // 学科简单列表
    subjectsSimple: {
      type: Array,
      required: true
    }
  },
  data () {
    return {
      // 新增目录数据标案
      form: {
        subjectID: '',
        directoryName: ''
      },
      //  验证规则
      rules: {
        directoryName: [
          { required: true, message: '请输入要添加的目录名称', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
  },
  computed: {},
  methods: {
    //  关闭弹出框
    onClose () {
      this.$refs.ruleForm.resetFields()
      this.form = {}
      this.$emit('update:isAddDirectoryDialog', false)
    },
    async DirectoryAdd () {
      try {
        await this.$refs.ruleForm.validate()
        if (this.$route.query.id) {
          await addDirectory({
            subjectID: this.$route.query.id * 1,
            directoryName: this.form.directoryName
          })
          this.$message.success('添加成功')
          this.$emit('update:isAddDirectoryDialog', false)
          this.$emit('loadDirectorsList')
        } else {
          await addDirectory(this.form)
          this.$message.success('添加成功')
          this.$emit('update:isAddDirectoryDialog', false)
          this.$emit('loadDirectorsList')
        }
      } catch (err) {
        console.log(err)
      }
    }
  }
}
</script>

<style lang='less' scoped>
.el-form {
  padding-bottom: 10px;

  .el-input {
    width: 200px;
  }
}

.isShow {
  display: none;
}
</style>
